package org.hqs.arrangement.controller;

import org.hqs.arrangement.common.JsonResult;
import org.hqs.arrangement.pojo.Arrangement;
import org.hqs.arrangement.service.ArrangementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/arrangement")
public class ArrangementController {

    private final ArrangementService arrangementService;

    @Autowired
    public ArrangementController(ArrangementService arrangementService) {
        this.arrangementService = arrangementService;
    }

    @GetMapping
    public JsonResult<List<Arrangement>> getAllArrangements() {
        return new JsonResult(arrangementService.getAllArrangements());
    }

    @GetMapping("/{id}")
    public JsonResult<Arrangement> getArrangementById(@PathVariable int id) {
        return new JsonResult(arrangementService.getArrangementById(id));
    }

    @PostMapping
    public JsonResult insertArrangement(@RequestBody Arrangement arrangement) {
        int res = arrangementService.insertArrangement(arrangement);
        if (res > 0) {
            return new JsonResult("200", "新增成功");
        }
        return new JsonResult("400", "似乎新增失败了");
    }

    @PutMapping("/{id}")
    public JsonResult updateArrangement(@PathVariable int id, @RequestBody Arrangement arrangement) {
        arrangement.setId(id);
        int res = arrangementService.updateArrangement(arrangement);
        if (res > 0) {
            return new JsonResult("200", "更改成功");
        }
        return new JsonResult("400", "似乎更改失败了");
    }

    @DeleteMapping("/{id}")
    public JsonResult deleteArrangementById(@PathVariable int id) {
        if (arrangementService.deleteArrangementById(id) > 0) {
            return new JsonResult("200", "删除成功");
        }
        return new JsonResult("400", "似乎删除失败了");

    }

    @GetMapping("/find_by_clinic_id/{id}")
    public JsonResult<List<Arrangement>> findByClinicId(@PathVariable int id){
        return new JsonResult(arrangementService.findArrangementByClinicId(id));
    }
}
